from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class CategoryPage:
    def __init__(self, driver):
        self.driver = driver
        # 页面元素定位器
        self.category_title_locator = (By.ID, "category-title")
        self.product_list_locator = (By.CLASS_NAME, "product-list")
        self.filter_button_locator = (By.ID, "filter-button")
        self.sort_by_locator = (By.ID, "sort-by")
        self.sort_options_locator = (By.XPATH, "//select[@id='sort-by']/option")
        self.price_slider_locator = (By.ID, "price-slider")
        self.view_more_button_locator = (By.ID, "view-more-button")
        self.product_card_locator = (By.CLASS_NAME, "product-card")
        self.selected_product_locator = (By.ID, "selected-product")
        self.breadcrumb_locator = (By.ID, "breadcrumb")

    def navigate(self, category):
        self.driver.get(f'http://example.com/category/{category}')

    def get_category_title(self):
        category_title = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located(self.category_title_locator)
        )
        return category_title.text

    def verify_product_list(self, expected_product_count):
        product_list = WebDriverWait(self.driver, 10).until(
            EC.presence_of_all_elements_located(self.product_list_locator)
        )
        return len(product_list) >= expected_product_count

    def filter_products(self, filter_criteria):
        filter_button = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.filter_button_locator)
        )
        filter_button.click()
        # Implement the logic to select filter criteria
        # For example, clicking on a specific checkbox or selecting an option from a dropdown

    def sort_products(self, sort_option):
        sort_by = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.sort_by_locator)
        )
        sort_by.click()
        options = WebDriverWait(self.driver, 10).until(
            EC.presence_of_all_elements_located(self.sort_options_locator)
        )
        for option in options:
            if option.text == sort_option:
                option.click()
                break

    def adjust_price_slider(self, min_price, max_price):
        price_slider = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.price_slider_locator)
        )
        # Implement the logic to adjust the price slider
        # This might involve using JavaScript executor or actions chains

    def click_view_more_button(self):
        view_more_button = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.view_more_button_locator)
        )
        view_more_button.click()

    def select_product(self, product_name):
        product_cards = WebDriverWait(self.driver, 10).until(
            EC.presence_of_all_elements_located(self.product_card_locator)
        )
        for card in product_cards:
            if product_name in card.text:
                card.click()
                break

    def verify_selected_product(self, product_name):
        selected_product = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.selected_product_locator)
        )
        return product_name in selected_product.text

    def verify_breadcrumb(self, breadcrumb_text):
        breadcrumb = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located(self.breadcrumb_locator)
        )
        return breadcrumb_text in breadcrumb.text